#!/usr/bin/env groovy

def kubectl_auth = "c26898c2-92c3-4c19-8490-9cf8ff7918ef"

pipeline {
    agent {
        kubernetes {
            yaml '''
apiVersion: v1
kind: Pod
metadata:
  name: jenkins-slave
  namespace: jenkins
spec:
  tolerations:
  - key: k8s-jenkins
    operator: Equal
    value: k8s-jenkins
    effect: NoSchedule
  containers:
  - name: kubectl
    #image: kostiscodefresh/kubectl-argo-rollouts:v1.6.0
    image: ccr.ccs.tencentyun.com/huanghuanhui/kubectl:v1.6.0
    imagePullPolicy: IfNotPresent
    command:
    - sleep
    args:
    - 99d
  - name: jnlp
    #image: jenkins/inbound-agent:3206.vb_15dcf73f6a_9-2
    image: ccr.ccs.tencentyun.com/huanghuanhui/jenkins-inbound-agent:3206.vb_15dcf73f6a_9-2
    imagePullPolicy: IfNotPresent
'''
        }
    }

environment {
AppName = "${AppName}"
}

    parameters {
        string(name: 'AppName', defaultValue: '', description: '服务名')
    }

    stages {
        stage('回滚') {
            steps {
              container('kubectl') {
              configFileProvider([configFile(fileId: "${kubectl_auth}", variable: 'kubeconfig')]) {
                sh """
                mkdir -p ~/.kube && cp ${kubeconfig} ~/.kube/config
                 /app/kubectl-argo-rollouts-linux-amd64 undo ${AppName} -n prod
                """
                   }
                }
            }
        }
    }
}

post {
        success {
            script {
                def message = """
                    {
                        "msgtype": "markdown",
                        "markdown": {
                            "content": "「stupid」发布系统通知
                            > 当前状态: <font color=\\\"warning\\\">构建成功</font>
                            > 提交信息: <font color=\\\"warning\\\">${COMMIT_INFO}</font>
                            > 提交人员: ${COMMIT_USER}
                            > 发布应用: <font color=\\\"warning\\\">${APPLICATION_NAME}</font>
                            > 提交时间: <font color=\\\"comment\\\">${COMMIT_TIME}</font>
                            > 当前版本: <font color=\\\"comment\\\">dev</font>
                        }
                    }
                """
                sh """
                    curl -s -d '${message}' -H 'Content-Type: application/json' -i -XPOST ${QYWX_URL}
                """
            }
        }
        failure {
            script {
                def message = """
                    {
                        "msgtype": "markdown",
                        "markdown": {
                            "content": "「stupid」发布系统通知
                            > 当前状态: <font color=\\\"warning\\\">构建失败</font>
                            > 提交信息: <font color=\\\"warning\\\">${COMMIT_INFO}</font>
                            > 提交人员: ${COMMIT_USER}
                            > 发布应用: <font color=\\\"warning\\\">${APPLICATION_NAME}</font>
                            > 提交时间: <font color=\\\"comment\\\">${COMMIT_TIME}</font>
                            > 当前版本: <font color=\\\"comment\\\">${params.GIT_BRANCH}</font>
                        }
                    }
                """
                sh """
                    curl -s -d '${message}' -H 'Content-Type: application/json' -i -XPOST https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=227da3b0-60e7-4b30-97fe-ed79a3268f31
                """
            }
        }
    }
}
